//
//  CLMainTabBarController.swift
//  CLWeibo
//
//  Created by 陈亮 on 16/7/9.
//  Copyright © 2016年 陈亮. All rights reserved.
//

import UIKit
import SVProgressHUD

class CLMainTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //设置自定义tabbar
        let clTabBar = CLTabBar()
        
        clTabBar.composeButtonClosure = { [weak self] in
            
            print("闭包传递撰写按钮点击")
            if !CLUserAccountViewModel.sharedUserAccount.isLogin {
                SVProgressHUD.showErrorWithStatus("您还没有登录哟")
                return
            }
//            let composeView = CLComposeView()
//            let window = UIApplication.sharedApplication().windows.last!
//            window.addSubview(composeView)
            if let weakSelf = self {
                let composeView = CLComposeView()
                composeView.show(weakSelf)
            }
        }
        setValue(clTabBar, forKey: "tabBar")
        //添加子控制器
        addChildViewController(CLHomeTableViewController(), title: "首页", imageName:"tabbar_home")
        addChildViewController(CLMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(CLDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(CLProfileTableViewController(), title: "我的", imageName: "tabbar_profile")
       
        
    }

    // MARK:  添加控制器
    func addChildViewController(childController: UIViewController,title :String,imageName :String) {
        //自定义tabBarItem
        childController.tabBarItem = CLTabBarItem()
        //设置tabbar的文字和图片
        childController.title = title
        childController.tabBarItem.image = UIImage(named: imageName)
        childController.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")
        //图片渲染
        childController.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        //设置tabbar的文字颜色
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: .Selected)
        //设置字体
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFontOfSize(14)], forState: .Normal)
        
        //添加导航控制器
        let nav = UINavigationController(rootViewController: childController)
        addChildViewController(nav)
        
    }

}
